package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyContentMapper dailyContentMapper;
    @Autowired
    private DailyReviewMapper reviewMapper;
    @Autowired
    private FavoritesMapper favoritesMapper;
    @Autowired
    private DailyCommentReplyMapper commentReplyMapper;

    @Override
    public PageInfo query(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Strategy> list = dailyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void saveOrUpdate(Daily daily) {
        Date date = new Date();
        if (daily.getId() == null) {
            daily.setLikes(0);
            daily.setDailyCreateTime(date);
            dailyMapper.insert(daily);
        } else {
            daily.setLastUpdateTime(date);
            dailyMapper.updateByPrimaryKey(daily);
        }
    }

    public DailyContent getContentById(Long id) {
        return dailyContentMapper.selectByPrimaryKey(id);
    }

    public Daily getById(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }

    public PageInfo<?> getDailyReviewsById(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<DailyReview> list=reviewMapper.getDailyReviewsById(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdateReview(DailyReview dailyReview) {
        Date date = new Date();
        dailyReview.setReviewTime(date);
        reviewMapper.insert(dailyReview);
    }

    public Favorites getFavoritesById(Favorites favorites) {
        Favorites favorites1 = favoritesMapper.getFavoritesById(favorites.getUserId(),favorites.getDailyId());
        return favorites1;
    }

    public void saveReply(DailyCommentReply commentReply) {
        commentReply.setCreateTime(new Date());
        commentReplyMapper.insert(commentReply);
    }

}